/*******************************************************************************
 * Copyright (c) 2012 Alexandr Tsvetkov.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Alexandr Tsvetkov - initial API and implementation
 * 
 * Project:
 *     TAO Library
 * 
 * File name:
 *     IconContextMenu.java
 *     
 * License agreement:
 *
 * 1. This code is published AS IS. Author is not responsible for any damage that can be
 *    caused by any application that uses this code.
 * 2. Author does not give a garantee, that this code is error free.
 * 3. This code can be used in NON-COMMERCIAL applications AS IS without any special
 *    permission from author.
 * 4. This code can be modified without any special permission from author IF AND ONLY IF
 *    this license agreement will remain unchanged.
 * 5. SPECIAL PERMISSION for this code usage in COMMERCIAL application SHOULD be obtained
 *    from author.
 ******************************************************************************/
package ua.at.tsvetkov.ui.menus;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.res.Resources;

/**
 * Создает контекстное меню расширяя класс Dialog. Создается как обычный диалог
 * @author A.Tsvetkov 2011, http://tsvetkov.at.ua, al@ukr.net
 *
 */
public class IconContextMenu implements DialogInterface.OnCancelListener, DialogInterface.OnDismissListener {


	private IconMenuAdapter						menuAdapter				= null;
	private Activity								parentActivity			= null;
	private int										dialogId					= 0;

	private IconContextMenuOnClickListener	clickHandler			= null;

	private Resources	res;

	/**
	 * Конструктор меню
	 * @param parent родительская Activity
	 * @param id идентификатор диалога
	 */
	public IconContextMenu(Activity parent, int id) {
		this.parentActivity = parent;
		this.dialogId = id;
		res = parent.getResources();
		menuAdapter = new IconMenuAdapter(parentActivity);
	}

	/**
	 * Добавляет пункт в меню
	 * @param title текст
	 * @param imageResourceId id изображения 
	 * @param actionTag номер вызываемого действия для обработки в методе <code>OnClickListener()</code>
	 */
	public void addItem(CharSequence title, int imageResourceId, int actionTag) {
		menuAdapter.addItem(new IconContextMenuItem(res, title, imageResourceId, actionTag));
	}

	/**
	 * Добавляет пункт в меню
	 * @param textResourceId id текстового ресурса
	 * @param imageResourceId id изображения 
	 * @param actionTag номер вызываемого действия для обработки в методе <code>OnClickListener()</code>
	 */
	public void addItem(int textResourceId, int imageResourceId, int actionTag) {
		menuAdapter.addItem(new IconContextMenuItem(res, textResourceId, imageResourceId, actionTag));
	}

	/**
	 * Назначает обработчик нажатия
	 * @param listener
	 */
	public void setOnClickListener(IconContextMenuOnClickListener listener) {
		clickHandler = listener;
	}

	/**
	 * Созданиe меню
	 * @return диалог-меню
	 */
	public Dialog createMenu(String menuItitle) {
		final AlertDialog.Builder builder = new AlertDialog.Builder(parentActivity);
		builder.setTitle(menuItitle);
		builder.setAdapter(menuAdapter, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface DialogInterface, int i) {
				IconContextMenuItem item = (IconContextMenuItem) menuAdapter.getItem(i);

				if (clickHandler != null) {
					clickHandler.onClick(item.actionTag);
				}
			}
		});

		builder.setInverseBackgroundForced(true);

		AlertDialog dialog = builder.create();
		dialog.setOnCancelListener(this);
		dialog.setOnDismissListener(this);
		return dialog;
	}

	/**
	 * Удаление диалога
	 */
	public void onCancel(DialogInterface dialog) {
		cleanup();
	}

	/**
	 * Скрытие диалога
	 */
	public void onDismiss(DialogInterface dialog) {
	}

	@SuppressWarnings("deprecation")
	private void cleanup() {
		parentActivity.dismissDialog(dialogId);
	}

	/**
	 * IconContextMenu обработчик нажатий - интерфейс
	 */
	public interface IconContextMenuOnClickListener {
		public abstract void onClick(int menuId);
	}

}
